#pragma once
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#define Tasknum 3

typedef void (*task_t)();


void _print()
{
    std::cout<<"I am _print."<<std::endl;
}

void _download()
{
    std::cout<<"I am _download."<<std::endl;
}

void _flush()
{
    std::cout<<"I am _flush."<<std::endl;
}

task_t tasks[Tasknum];

void _inittask()
{
    srand(time(nullptr)^getpid());
    tasks[0]=_print;
    tasks[1]=_download;
    tasks[2]=_flush;
}

void _excutetask(int tnum)
{
    if(tnum<0 ||tnum>2)
        return;
    tasks[tnum]();
}

int _selecttask()
{
    return rand()%Tasknum;
}

void _work()
{
    while (true)
    {
        int command = 0;
        int n = read(0, &command, sizeof(command));
        if (n == sizeof(int))
        {
            std::cout << "pid is:" << getpid() << " handle task" << std::endl;
            _excutetask(command);
        }
        else if (n == 0)
        {
            std::cout << "sub process:" << getpid() << "quit" << std::endl;
            break;
        }
    }
}
